<?php
class UserAction extends BaseAction {
	protected $User,$site,$roledao,$roleuserdao,$userextdao;
	protected $superadmin;
	function _initialize() {
		parent::_initialize ();
		$this->dao = M ( 'User' );
		$this->userextdao = M('UserExt');
		$this->roledao = M('Role');
		$this->roleuserdao = M('RoleUser');
		import("Com.User");
		$this->User = new User('SMW01');
		import("Com.Site");
		$this->site = new Site();
		//超级管理员Id
		$this->superadmin = array(1111);
		
	}
	/**
	 * 用户综合列表
	 */
	function index() {
		//用户组
		$this->rolelist();
		$where = array();
		//刷选角色
		if($_REQUEST['usergroup']!='all' && $_REQUEST['usergroup']){
			$uids = self::groupuser($_REQUEST['usergroup']);
			if($uids){
				$where['id'] = array('IN',implode($uids, ','));
			}else{
				$this->display ();
			}
		}
		
		$condition = parent::create_where ($where);
		$count = $this->dao->where ( $condition )->count ();
// 		if($_GET['iic']="demo"){
// 			dump($this->dao->getLastSql());
// 		}
		$this->assign ( "totalCount", $count );
		if ($count > 0) {
			$page = new Page ( $count, $this->listrows );
			$list = $this->dao->where ( $condition )->order ( $this->order )->limit ( $page->firstRow . ',' . $page->listRows )->select ();
			$this->assign ( 'list', $list );
		}

		$this->assign('roleid',$_REQUEST['usergroup']);
		$this->display ();
	}
	
	/**
	 * 获取用户组信息
	 */
	function rolelist($group = ''){
		if($group){
			$where['group'] = is_array($group) ? array('IN',implode(',', $group)) : $group;
		}
		//获取用户组信息
		$rolelist = $this->roledao->where($where)->order('`group`')->select();
		$this->assign('usergroup',$rolelist);
	}
	
	/**
	 * 通过权限刷选用户id
	 */
	function groupuser($roleid){
		$map['role_id'] = $roleid;
		$res = $this->roleuserdao->field('user_id')->where($map)->select();
		$res = self::_arr2to1($res,'user_id');
		return $res;
		
	}
	
	function add() {
		//获取用户id
		$id = $_REQUEST['id'];
		if($id){
			$user = $this->dao->find($id);
			$this->assign('info',$user);
			
			//云采购数据
			//$user_smw = M("UserB2b")->where('user_id='.$id)->find();
			//$this->assign('user_smw',$user_smw);
		}
		//获取用户组信息
		$this->rolelist(array(1,3));
		//获取退款类型
		$platform = M("SystemConfig")->field('value,title')->where('element="platform"')->select();
		
		//获取地区列表
		$provinces = $this->site->get_area(0);
		$this->assign('provinces',$provinces);
		$this->assign('platform',$platform);
		$this->assign('type',$_REQUEST['type']);
		$this->assign('userrole',$this->roles[0]);
		$this->display ();
		
	}
	
	function insert(){
		$id = $_REQUEST['id'];
		$base = $_REQUEST['base'];
		$user = $base;
		unset($user['password']); //删除第二个元素 
		$role_id = $_REQUEST['role'];
		$platform = 'SMW01';
		$this->User = new User($platform);
		if($id){
			$user['id'] = $id;
			$res = $this->User->user_edit('user',$user,array('username'=>'用户名','email' => '信箱','mobile' => '手机'),array('username'=>'用户名','email' => '信箱','mobile' => '手机'));
			$res = json_decode($res);
			
		}else{
			
			$res = json_decode($this->User->user_add($base,$user,$base,$role_id));
			
		}
		
		if($res->status == 1){
			//添加用户组
		
			$this->_ajm(200,'保存成功！');
		}else{
			$this->_ajm(300,$res->msg);
		}
	}
	
	/*
	 * 平台角色信息
	 * */
	function platform_role(){
		$arr=array();
		$platform =  $_REQUEST ['platform'];
		if(empty($platform)){
			$arr = array('','请选择');
			exit(json_encode($arr));
		}
		$where['platform']  = $platform;
		//获取用户组信息
		$rolelist = $this->roledao->field('id,name')->where($where)->order('`group`,id')->select();//echo $this->roledao->getLastSql();
		foreach ($rolelist as $r){			
			$arr[$r['id']] = array($r['id'],$r['name']);
		}
		
		//超级管理员，才能添加超级管理员
		if(!in_array ( intval($this->userid), $this->superadmin, true )){
			unset($arr[1]);
		}
		
		
		exit(json_encode($arr));
	}
	
	/**
	 * 云采购平台的用户数据
	 */
	function smw() {
		//用户组
		$this->rolelist();
		$dao = D('UserV');
		$where = array ();
		$where ['e_element'] = 'status';
		$where ['e_platform'] = 'SMW01';
		//刷选角色
		if($_REQUEST['usergroup']!='all' && $_REQUEST['usergroup']){
			$uids = self::groupuser($_REQUEST['usergroup']);
			if($uids){
				$where['userid'] = array('IN',implode($uids, ','));
			}else{
				$this->display ();
			}
			$this->assign('roleid',$_REQUEST['usergroup']);
		}
		
		$condition = parent::create_where ( $where );
		$order = $this->orderField . ' ' . $this->orderDirection;
		$count = $dao->where ( $condition )->count ();
// 		echo $dao->getLastSql();
		if($_GET['iic']=='demo'){
		dump($dao->getLastSql());
		}
		$this->assign ( "totalCount", $count );
		if ($count > 0) {
			$page = new Page ( $count, $this->listrows );
			$list = $dao->where ( $condition )->order ( $order )->limit ( $page->firstRow . ',' . $page->listRows )->select ();
			$b2b = self::user_b2b ( $list );
			$userrole = self::user_role( $list,'0');
			$this->assign ( 'list', $list );
			$this->assign ( 'b2b', $b2b );
			if($_GET['iic']=='demo'){
				dump($userrole);
			}
			$this->assign('userrole',$userrole);
		}

		$this->assign('where',$where);
		$this->display ();
	}
	/**
	 * O2O平台的用户
	 */
	function o2o() {
		//用户组
		$this->rolelist();
		
		$dao = D('UserV');
		$where = array ();
		$where ['e_element'] = 'status';
		$where ['e_platform'] = '91YUNGOU';
		//刷选角色
		if($_REQUEST['usergroup']!='all' && $_REQUEST['usergroup']){
			$uids = self::groupuser($_REQUEST['usergroup']);
			if($uids){
				$where['userid'] = array('IN',implode($uids, ','));
			}else{
				$this->display ();
			}
			$this->assign('roleid',$_REQUEST['usergroup']);
		}
				
		$condition = parent::create_where ( $where );
		$order = $this->orderField . ' ' . $this->orderDirection;
		$count = $dao->where ( $condition )->count ();
		$this->assign ( "totalCount", $count );
		if ($count > 0) {
			$page = new Page ( $count, $this->listrows );
			$list = $dao->where ( $condition )->order ( $order )->limit ( $page->firstRow . ',' . $page->listRows )->select ();
			$o2o = self::user_o2o ( $list );
			$this->assign ( 'list', $list );
			$this->assign ( 'o2o', $o2o );
		}
		
		
		
		$this->assign('where',$where);
		$this->display ();
	}
	
	/**
	 * 内部用户
	 */
	function admin() {
		$map['group'] = 1;
		$role = $this->roledao->where($map)->select();
		$role = $this->_arr2to1($role,'id');
		$where = array();
		$where['role_id'] = array('IN',implode(',', $role));
		$where = self::create_where($where);
		$count = $this->roleuserdao->where($where)->join(' as ru left join user as u on u.id = ru.user_id')->count();
		$this->assign ( "totalCount", $count );
		if($count){
			$page = new Page ( $count, $this->listrows );
			$list = $this->roleuserdao->where($where)->join(' as ru left join user as u on u.id = ru.user_id')->limit( $page->firstRow . ',' . $page->listRows )->select();
			//echo $this->roleuserdao->getLastSql();exit;
			$this->assign('list',$list);
		}
		$this->display();
	}
	
	/**
	 * 获取用户的扩展信息
	 */
	function _get_ext($data = array(), $type = 'all') {
		$res = array ();
		$uids = array ();
		$ext = M ( "UserExt" );
		$money = M ( "UserMoney" );
		foreach ( $data as $v ) {
			$uids [] = $v ['id'];
			$res [$v ['id']] = $v;
			$extstatus = $ext->field ( 'GROUP_CONCAT(`platform`) plat' )->where ( 'userid="' . $v ['id'] . '" AND `element`="status"' )->select ();
			if ($extstatus) {
				$extstatus = explode ( ',', $extstatus ['0'] ['plat'] );
				if ($type == 'all' || $type == 'ext') {
					if (in_array ( 'SMW01', $extstatus )) {
						$res [$v ['id']] ['ext'] ['SMW01'] = $ext->where ( 'userid=' . $v ['id'] . ' AND `platform`="SMW01"' )->select ();
					}
					if (in_array ( '91YUNGOU', $extstatus )) {
						$res [$v ['id']] ['ext'] ['91YUNGOU'] = $ext->where ( 'userid=' . $v ['id'] . ' AND `platform`="91YUNGOU"' )->select ();
					}
				}
				if ($type == 'all' || $type == 'money') {
					if (in_array ( 'SMW01', $extstatus )) {
						$res [$v ['id']] ['money'] ['SMW01'] = $money->where ( 'userid=' . $v ['id'] . ' AND `platform`="SMW01"' )->select ();
					}
					if (in_array ( '91YUNGOU', $extstatus )) {
						$res [$v ['id']] ['money'] ['91YUNGOU'] = $money->where ( 'userid=' . $v ['id'] . ' AND `platform`="91YUNGOU"' )->select ();
					}
				}
			}
		}
		$ids = implode ( ',', $uids );
		return $res;
	}
	
	
	/**
	 * 获取用户角色信息
	 * *@date 2014.02.28
	 * *@time 下午4:01:33
	 */
	function user_role($user = array(),$group=2) {
		// 获取用户B2B平台的信息
		$dao = D ( 'RoleUser' );
		if (is_array ( $user )) {
			foreach ( $user as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $user;
		}
		$where = array ();
		$where ['user_id'] = array (
				'IN',
				implode ( ',', $ids )
		);
		if($group!=0){
			$where['group'] =$group;
		}
		$res = array ();
		$data = $dao->join(' as ur left join role as r on r.id = ur.role_id ')->where ( $where )->select ();
		if (is_array ( $user )) {
			foreach ( $data as $v ) {
				$res [$v ['user_id']] = $v;
			}
		} else {
			$res = $data;
		}
		return $res;
	} // end user_role
	
	/**
	 * 获取用户B2B平台的信息
	 * *@date 2013-10-14
	 * *@time 下午4:01:33
	 */
	function user_b2b($user = array()) {
		// 获取用户B2B平台的信息
		$dao = D ( 'UserB2b' );
		if (is_array ( $user )) {
			foreach ( $user as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $user;
		}
		$where = array ();
		$where ['user_id'] = array (
				'IN',
				implode ( ',', $ids ) 
		);
		$res = array ();
		$data = $dao->where ( $where )->select ();
		if (is_array ( $user )) {
			foreach ( $data as $v ) {
				$res [$v ['user_id']] = $v;
			}
		} else {
			$res = $data;
		}
		return $res;
	} // end user_b2b
	
	
	
	/**
	 * 获取用户云购平台的信息
	 * *@date 2013-10-14
	 * *@time 下午4:01:33
	 */
	function user_o2o($user = array()) {
		// 获取用户B2B平台的信息
		$dao = D ( 'UserO2o' );
		if (is_array ( $user )) {
			foreach ( $user as $v ) {
				$ids [] = $v ['id'];
			}
		} else {
			$ids [] = $user;
		}
		$where = array ();
		$where ['user_id'] = array (
				'IN',
				implode ( ',', $ids ) 
		);
		$res = array ();
		$data = $dao->where ( $where )->select ();
		if (is_array ( $user )) {
			foreach ( $data as $v ) {
				$res [$v ['user_id']] ['91YUNGOU'] = $v;
			}
		} else {
			$res = $data;
		}
		return $res;
	} // end user_o2o
	
	/**
	 * 查看用户的详情
	 */
	function detail() {
		$dao = $this->dao;
		$pk = $dao->getPk ();
		$where = array ();
		$id = intval ( $_REQUEST ['id'] );
		if (empty ( $id )) {
			$this->_ajm ( 300, '请选择一条数据查看', 0 );
		}
		$where [$pk] = array (
				'eq',
				$id 
		);
		$res = $dao->where ( $where )->find ();
		if (empty ( $res )) {
			$this->_ajm ( 300, '查询数据出错或者查询的数据不存在', 0 );
		}
		$res = self::_get_ext ( array (
				$res 
		) );
		
		//获取用户角色信息
		$RoleUser = M('RoleUser');
		$roleuser = $RoleUser->field('role_id,name,platform')->join(' as ru left join role as r on r.id = ru.role_id')->where('user_id='.$id)->select();
		//获取该用户地址
		$extinfo = $this->User->get_data("UserAddress", array('userid' => $id), '', true);
		foreach ($extinfo as $key=>$value){
			$value['pro'] = $this->get_areaname($value['province']);
			$value['ci'] = $this->get_areaname($value['city']);
			$value['ar'] = $this->get_areaname($value['area']);
			$address[$value['platform']][] = $value;
		}

		$this->assign ( "info", $res [$id] );
		$this->assign('address',$address);
		$this->assign('roleuser',$roleuser);
		$this->assign ( "readonly", 'disabled="true"' );
		$this->display ();
	}//end 查看用户详情
	
	

	/**
	 * 用户组列表
	 */
	function group(){
		$dao = M("role");
		$order = $this->orderField . ' ' . $this->orderDirection;
		$res = $dao->order($order)->select();
		$this->assign('list',$res);
		$this->display();
	}
	
	
	
	/**
	 * 用户审核
	 */
	function userverify() {
		$groupname = array(
			'1' => '后台管理角色',
			'2' => '分销商角色',
			'3' => '代理商角色',
			'4' => '套件角色',
			'5' => '云购角色'
		);
		//云采购 
		$group = '2,3,4,5';
		
		$id = $_REQUEST['id'];
		//获取用户状态
		$user = $this->User->get_data('User', array('id' => $id));

		//获取用户角色信息
		$where = array();
		$where['user_id'] = $id;
		//$where['group'] = $group;
		$user_role = $this->roleuserdao->field('role_id')->join(' as ru left join role as r on r.id = ru.role_id')->where($where)->select();
		$user_role = field2key($user_role,'role_id');
		
		//获取用户组信息
		//获取用户组信息
		$where = array();
		$where['group'] = array('in',$group);
		$grouplist = $this->roledao->field('`group`')->where($where)->group('`group`')->select();
		//echo $this->roledao->getLastSql();
		$rolelist = $this->roledao->where($where)->order('`group`')->select();
		$rolels=array();
		foreach ($rolelist as $vo){
			if($vo['id'] != 6 ){
				$rolels[] = $vo;
			}
		}
		
		
		
		foreach ($grouplist as $g){
			foreach ($rolels as $vo){
				if($vo['group'] == $g['group']){
					$rolegroup[$g['group']][] = $vo;
				}
			}
		}
		//print_r($rolegroup);
		
		$this->assign('group',$group);
		$this->assign('user',$user);
		$this->assign('rolegroup',$rolegroup);
		$this->assign('groupname',$groupname);
		$this->assign('userrole',$user_role);
		$this->display();
	}
	
	/**
	 * 用户审核
	 */
	function verify(){
		//更新user状态
		$this->status();
		//更新userext 状态
	//	$this->userext_status();
		//更新用户组
		$this->role_user();
		$this->_ajm(200,'操作成功！',0,U ( 'User/smw' ));
	}
	
	/**
	 * 用户修改状态
	 */
	function status(){
		//更新user状态
		$this->user_status();
		//更新userext 状态
		$this->userext_status();
		if($_REQUEST['url'] == 1){
			$this->_ajm(200,'操作成功！',1);
		}
	}
	

	/**
	 *更新user状态
	 */
	function user_status(){
		$id = $_REQUEST['id'];
		$status = $_REQUEST['status'];

		if($id){
			$data['id'] = $id;
			$data['status'] = $status;
			$reid = $this->dao->save($data);
			if($reid>=0){
				return $reid;
			}else{
				$this->_ajm(300,'修改用户状态失败！');
			}
		}else{
			$this->_ajm(300,'该用户不存在!');
		}
		
	}
	
	/**
	 * 更新userext 状态
	 */
	function userext_status(){
		$id = $_REQUEST['id'];
		$status = $_REQUEST['status'];
		$where['userid'] = $id;
		$where['element'] = 'status';
		$where['platform'] = 'SMW01';
		if($id){
			//$data['userid'] = $id;
			$data['value'] = $status;
			$reid = $this->userextdao->where($where)->save($data);
			if($reid>=0){
				return $reid;
			}else{
				$this->_ajm(300,'修改用户扩展状态失败！');
			}
		}else{
			$this->_ajm(300,'该用户不存在!');
		}
	
	}
	
	
	
	/**
	 * 添加用户角色
	 */
	function role_user(){
		$id = $_REQUEST['id'];
		$group = $_REQUEST['group'];	
		$group = explode(',', $group);
		
		
		
		foreach($group as $g){
			//if($_POST['group_'.$g]){
				$roleids[$g] = $_POST['group_'.$g];
				//填写了角色的group 刷选出来
				$group_arr[] = $g;
				
			//}
		}
		
		//$where['group'] = array('in',implode(',', $group_arr));
		$where['group'] = array('in',$group);
		$rolelist = $this->roledao->field('id,`group`')->where($where)->order('`group`')->select();
		foreach ($rolelist as $vo){
			$grouprole[$vo['group']][] =$vo['id'];
		}

		foreach($grouprole as $key=>$gr){
			$map['user_id'] = $id;
			$map['role_id'] = array('in',implode(',', $gr));
	
			if($roleids[$key]){			
				$ug = $this->roleuserdao->where($map)->find();
				$data= array();
				$data['user_id'] = $id;
				$data['role_id'] = $roleids[$key];
				if($ug){
					//修改
					$data['id'] = $ug['id'];
					$data['mtime'] = time();
					$reid = $this->roleuserdao->save($data);
					if($reid<0){
						$this->_ajm(300,'修改角色失败！');
					}
				}else{
					//添加
					$data['ctime'] = time();
					$reid = $this->roleuserdao->add($data);
					if(!$reid){
						$this->_ajm(300,'添加角色失败!');
					}
				}
			
			}else{
				//当分组中角色为空的时候，就删除。
				//删除在这个组中的角色				
				$this->roleuserdao->where($map)->delete();
			}
		}

	}
	
	
	
	/**
	 * 添加用户角色(一个用户多个角色处理)
	 * 暂时无用，勿删
	 */
	function role_user_m(){
		$id = $_REQUEST['id'];
		$arr_role = $_REQUEST['role']; 
		$role = is_array($arr_role) ? implode(',', $arr_role) : $arr_role;   
		$map['user_id'] = $id;
		$map['role_id'] = array('IN',$role);
		$dao = M("roleUser");
		$ug = $dao->where($map)->select();
		$ug_role = array();
		if($ug){
			$ug_role = $this->_arr2to1($ug,'role_id');
			//删除原有的角色
		}
		//查找用户没有的角色
		$ug_diff = empty($ug_role) ? $arr_role : array_diff($arr_role,$ug_role);
		if($ug_diff){
			$data['user_id'] = $id;
			$data['ctime'] = time();
			foreach ($ug_diff as $roleid){
				$data['role_id'] = $roleid;
				$reid = $dao->add($data);
			}	
		}		
		return $reid;		
	}
	
	
	/*
	 * 用户修改密码
	 */
	function user_pwd(){
		if(!$this->userid){
			$this->_ajm(300,'请确保在登陆状态!');
		}
		if($_POST['newpwd'] != $_POST['repwd']){
			$this->_ajm(300,'新密码 和 重复新密码 不一致！');
			exit;
		}
		
		$user['id'] = $this->userid;
		$user['password'] = $_POST['newpwd'];		
		$field_null['password'] = $_POST['newpwd'];
		$old_pwd['password'] = $_POST['oldpwd'];
		
		$res = $this->User->password_edit($user,$field_null,$old_pwd);
		if($res){
			$res = json_decode($res);
			if($res->status==1){
				$this->_ajm(200,'修改密码成功！');
				break;
			}else{
				$this->_ajm(300,$res->msg);
			}
		}
		
		
	}

	

	/**
	 * 手机充值密码操作
	 */
	function pwd_edit(){
		$this->vcw= array(
				'adminwei','zhangruizhuan'
		);
		
		// add by tb 暂时
		if (!in_array( $this->username, $this->vcw, true ) ) {
			$this->_ajm ( 300, '权限不足!', 0 );
			exit;
		}
		
		$platform = '91YUNGOU';
		$id = $_REQUEST['id'];
		if(!$id){
			$this->_ajm(300,'没有此用户!');
		}
		
		/*if($platform == '91YUNGOU'){
			$dao = D ( 'UserO2o' );
			$usero2o = $dao->field('mobile')->where('userid='.$id)->find();
		}*/
		
		$dao = D ( 'User' );
		$res = $dao->field('mobile')->where('id='.$id)->find();
		
		//随机生成密码
		$pwd = rand(100000,999999);
		//$pwd = 123456;
		//修改数据库密码
		$user['id'] = $id;
		$user['password'] = md5($pwd);
		$status = json_decode($this->User->password_reset($user)); 
		if($status->status == 1){
			//发送短信
			$str='您在'.date('m月d日H时i分').'重置的密码是：'.$pwd .',登陆后。请尽快修改密码！';
			$msg=iconv ( "UTF-8", "GB2312//TRANSLIT",$str );
			$s = sendMobiMsg($res['mobile'],$msg);
			switch($s){
				case '1':
					$this->_ajm(300,'修改密码成功！短信成功发送！重置的密码是：'.$pwd);
					break;
				case '0':
					$this->_ajm(300,'修改密码成功！短信发送失败！请联系相关人员！'.$pwd);
					break;
				case '-99':
					$this->_ajm(300,'修改密码成功！短信账号余额不足，无法发送短信！'.$pwd);
					break;
				default:
					$this->_ajm(300,'短信接口无返回值，不能确定短信是否发送成功!'.$pwd);
			}
		}else{
			$this->_ajm(300,'修改密码失败！请重试！');
		}

	}
	
	/**
	 * 充值密码操作
	 */
	function info_edit(){
		$type = $_REQUEST['type'];
		$id = $_GET['id'];
		$acname = $_GET['acname'];
		if(!$id){
			$this->_ajm(300,'没有此用户');
		}
		$dao = M("User");
		//$user = $dao->join(' as ub left join user as u  on u.id = ub.user_id ')->field('u.id,u.username,u.email,ub.mobile')->where('user_id='.$id)->find();
		$user = $dao->find($id);
		$this->assign('type',$type);
		$this->assign('user',$user);
		$this->assign('acname',$acname);
		$this->display();
	}
	
	
	/*
	 * 保存信息 
	*/
	function info_update(){
		$id = $_REQUEST['id'];
		$type = $_REQUEST['type'];		
		$tablename = "User";
		$user['id'] = $id;
		$user[$type] = $_REQUEST[$type];
		$field_exist[$type] = $_REQUEST[$type];
		$res = $this->User->user_edit($tablename,$user,$field_exist); //print_r($res);
		$res = json_decode($res);
		if($res->status == 1){
			$this->_ajm(200,$res->msg);
		}else{
			$this->_ajm(300,$res->msg);
		}
		//echo $res->status;
		
	}
	
	
	/**
	 *定制suggest
	 *@date 2013-9-27
	 *@time 下午2:27:48
	 */
	function suggest() {
		//定制suggest
		$where=array();
		if($_REQUEST['name']){
			$where=array('name'=>$_REQUEST['name']);
			$list=parent::suggest($where,true);
			$res=array();
			foreach ($list as $v){
				$res[]=array($v['id'],$v['value']);
			}
			echo json_encode($res);
		}else{
			parent::suggest($where);
		}
	
	}//end suggest
	/**
	 * 优惠活动设置页面
	 *@date 2013-10-24
	 *@time 下午2:19:53
	 */
	function youhui() {
		//优惠活动设置页面
// 		$ids='';
// 		if(!empty ($_REQUEST ['ids'])){
// 			$ids =  explode(',',$_REQUEST['ids']);
// 			foreach ( $ids as $id ) {
// 				$ases [] = $this->ods->order_detail ( $id );
// 			}
// 		}else{
// 			$id = $_REQUEST ['id'];
		
// 		if(is_array($_REQUEST['ids'])){
			$ids=implode(',', $_REQUEST['userids']);
// 			echo json_encode(array('action'=>'youhui','idarr'=>implode(',',$_REQUEST['userids'])));
// 			die();
			
// 		}
		$this->assign("idarr",$ids);
		$this->display();
	}//end youhui
	
	
	/*
	 * 用户收货地址
	 * @author tb
	 * @date 2014.04.07
	 * **/
	function ajaxaddr(){
		$uid = $_REQUEST['uid'];
		$model = M("UserAddress ua");
		$where['userid'] = $uid;
		$field = "ua.id,ua.name ,ua.address,ua.zipcode,ua.tel,ua.mobile ,(select `name` from area where ua.province = id) as province,(select `name` from area where ua.city = id) as city ,(select `name` from area where ua.area = id) as area";
		// SELECT ua.id,ua.address,ua.zipcode,ua.tel,ua.mobile ,(select `name` from area where ua.province = id) as province,(select `name` from area where ua.city = id) as city ,(select `name` from area where ua.area = id) as area from  user_address as ua
		$res = $model->field($field)->where($where)->select();
		$this->ajaxReturn($res);
	}
	
	/**
	 * 设置用户可以参与优惠活动
	 *@date 2013-10-24
	 *@time 下午2:06:18
	 */
	function set_youhui() {
		//设置用户可以参与优惠活动
		$uid=$_REQUEST['uid'];
		
		if($_REQUEST['uid'] == null || $_REQUEST['type'] == null){
			$this->_ajm(300,'请选择用户和活动类型！');
		}
		
		if(strpos($uid,',')!==false){
			$uid=explode(',',$uid);
		}
		$data=array();
		$data['type']=$_REQUEST['type'];
		$msg='';
		if($data['type']=='YH14032WBAK2W'){
			$msg="尊敬的联酷云采购客户，很荣幸的通知您，您获得一次充20000CV送20000CV活动的参与机会，现在请您通过电脑登陆云采购会员中心,按网站提示完成操作即可参与。";
		}elseif($data['type']=='YH14034WBAK5W'){
			$msg="尊敬的联酷云采购客户，很荣幸的通知您，您获得一次充50000CV送50000CV活动的参与机会，现在请您通过电脑登陆云采购会员中心,按网站提示完成操作即可参与。";
		}elseif($data['type']=='YH140310WBAK10W'){
			$msg="尊敬的联酷云采购客户，很荣幸的通知您，您获得一次充100000CV送100000CV活动的参与机会，现在请您通过电脑登陆云采购会员中心,按网站提示完成操作即可参与。";
		}elseif ($data['type']=='YH1403ALLWBAKALLW'){
			$msg="尊敬的联酷云采购客户，很荣幸的通知您，您获得一次预存充值送等额CV、手机活动的参与机会，现在请您通过电脑登陆云采购会员中心,按网站提示完成操作即可参与。";
		}
		$data['num']='1';
		$data['ctime']=time();
		$user=M('User');
		$dao=M('UserYouhui');
		if(is_array($uid)){
			foreach ($uid as $v){
				$userinfo=array();
				$userinfo=$user->field('username,mobile')->find($v);
				$data['userid']=intval($v);
				$res=false;
				$res=$dao->add($data,array(),true);
				if($res){
					$msgstr=iconv ( "UTF-8", "GB2312//TRANSLIT",$msg );
					sendMobiMsg($userinfo['mobile'],$msgstr);
				}
			}
		}else{
			$userinfo=array();
			$userinfo=$user->field('username,mobile')->find($uid);
			$data['userid']=intval($uid);
			$res=false;
			$res=$dao->add($data,array(),true);
			if($res){
				$msgstr=iconv ( "UTF-8", "GB2312//TRANSLIT",$msg );
				sendMobiMsg($userinfo['mobile'],$msgstr);
			}
		}
		if($res){
			$this->_ajm(200,'活动信息发送成功！');
		}else{
			$this->_ajm(300,'活动信息发送失败');
		}
	}//end set_youhui
	
	//客户免邮列表
	function free(){
		$user_free= M('UserFreeMail');
		
		$where=array();
		if ($_REQUEST['userid']) {
			$where['userid']=$_REQUEST['userid'];
			$this->assign ( 'userid', $_REQUEST['userid'] );
		}
		$count = $user_free->where($where)->count ();
		$this->assign ( "totalCount", $count );
		if ($count > 0) {
			$page = new Page ( $count, $this->listrows );
			$pages = $page->show ();
			$list = $user_free->where($where)->order('id desc')->limit ( $page->firstRow . ',' . $page->listRows )->select();
			$this->assign ( 'list', $list );
		}
		
		$this->display ();
		
	}
	//添加免邮客户界面
	function add_free(){
// 		$user_free= M('UserFreeMail');
	
		$this->display ();
	
	}
	//添加免邮客户
	function insert_free(){
		
		//条件验证
		if(strtotime($_REQUEST['starttime'].'00:00:00') > strtotime($_REQUEST['endtime'].' 23:59:59')){
			$this->_ajm(300,'免邮有效开始时间不能大于免邮有效结束时间');
		}
		$user_free= M('UserFreeMail');
		$data=$_REQUEST;
		$data['starttime']=strtotime($_REQUEST['starttime'].'00:00:00');
		$data['endtime']=strtotime($_REQUEST['endtime'].' 23:59:59');
		$data['ctime']=time();
		$data['userid']=$data['user_id'];
		$data['surplus_number']=$data['number'];
		$res=$user_free->add($data);
		if($res){
			$this->_ajm(200,'免邮用户添加成功！');
		}else{
			$this->_ajm(300,'免邮用户添加失败');
		}
	
	}
	//删除免邮用户
	function delete_free(){
		$user_free= M('UserFreeMail');
		$data=array();
		$data['state']=0;
		$res=$user_free->where(array('id'=>$_REQUEST['id']))->save($data);
// 		$res=$user_free->where(array('id'=>$_REQUEST['id']))->delete();
		if($res===FALSE){
			$this->_ajm(300,'操作错误，请重试');
		}else{
			$this->_ajm(200,'操作成功',1,U(MODULE_NAME.'/free'));
		}
	}
	
	
}//end class